package com.film.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.film.domain.FilmOrder;
import com.film.domain.UserOrder;
import com.film.service.OrderService;
import com.film.service.impl.OrderServiceImpl;

/*创建订单 6.12 23.37*/
@WebServlet("/createOrder.do")
public class CreateOrderController extends HttpServlet{
	
	private OrderService orderService = new OrderServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//座位个数
		String count = request.getParameter("count");
		List<Integer> seatIds = new ArrayList<>();
		for (int i = 0; i < Integer.parseInt(count); i++) {
			seatIds.add(Integer.parseInt((request.getParameter("seatId"+i))));
		}
		String showingsId = request.getParameter("showingsId");
		String uid  = request.getParameter("uid");
		String orderNum = orderService.createOrder(Integer.parseInt(showingsId), Integer.parseInt(uid), seatIds);
		List<FilmOrder> currentOrders = orderService.selectOrder(orderNum);
		List<UserOrder> currOrders = orderService.arrangeList(currentOrders);
		HttpSession session = request.getSession();
		session.setAttribute("currOrders", currOrders);
		response.sendRedirect("pay.jsp");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}
	
}
